package com.mlt.gwt.test.server;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.SettingsFactory;

import com.mlt.gwt.test.server.domain.Appuntamento;

public class HibernateUtil {

    private static final SessionFactory sessionFactory1 = buildSessionFactory("drive_hd.cfg.xml");
    private static final SessionFactory sessionFactory2 = buildSessionFactory("drive_usb.cfg.xml");

    private static SessionFactory buildSessionFactory(String cfgName) {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
//MLT            return new Configuration().configure().buildSessionFactory();
        	
        	SessionFactory f = new AnnotationConfiguration().configure(cfgName).addPackage("com.mlt.gwt.test.server.domain").addAnnotatedClass(Appuntamento.class).buildSessionFactory();
        	return f;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory_hd() {
        return sessionFactory1;
    }

    public static SessionFactory getSessionFactory_usb() {
        return sessionFactory2;
    }


    public static Session getSession_hd() {
    	if(sessionFactory1.getCurrentSession().isOpen())
    		return sessionFactory1.getCurrentSession();
    	else
    		return sessionFactory1.openSession();
    }

    public static Session getSession_usb() {
    	if(sessionFactory2.getCurrentSession().isOpen())
    		return sessionFactory2.getCurrentSession();
    	else
    		return sessionFactory2.openSession();
    }

    
}
